package com.finalcial.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.finalcial.dao.EchatsDao;
import com.finalcial.service.EchatsService;

@Service
public class EchatsServiceImpl implements EchatsService{
	@Autowired
	private EchatsDao echatsDao;
	
	@Override
	public List<Map<String, Object>> pandectSalary(Integer userId,
		String costTypeName, String reimbursementTypeName) {
		Integer totalSalary = echatsDao.totalSalary(userId, costTypeName);//总额度
		Integer applySalary = echatsDao.applySalary(userId, reimbursementTypeName);//申请成功
		Integer applyIngSalary = echatsDao.applyIngSalary(userId, reimbursementTypeName);//正在申请
		Integer surplusSalary = totalSalary-(applySalary+applyIngSalary);//剩余金额
		List<Map<String, Object>> listSalary = new ArrayList<Map<String, Object>>(); 
		Map<String, Object> map = new HashMap<String, Object>();  
		map.put("totalSalary", totalSalary);  
		map.put("applySalary", applySalary);
		map.put("surplusSalary", surplusSalary);
		map.put("applyIngSalary", applyIngSalary);
		listSalary.add(map);
		return listSalary;
	}

	@Override
	public List<Map<String, Object>> listPersonalAccount(Integer userId) {
		//把所有的list存储进list
		List<Map<String, Object>> listPersonalAccount = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>(); 
		List<Map<String, Object>> getReimbursementTypeName = echatsDao.getReimbursementTypeName(userId);
		//得到报销的时间 list
		List<Date> getReimbursementTime = echatsDao.getReimbursementTime(userId);
		
		
		//报销类型的名称
		String reimbursementTypeName = "";
		
		//存储国内差旅
		List<Map<String, Object>> listDomesticTravel = new ArrayList<Map<String,Object>>();
	    Map<String, Object> domesticTravelMap = new HashMap<String, Object>(); 
	    //员工福利
	    List<Map<String, Object>> listStaffForm = new ArrayList<Map<String,Object>>();
	    Map<String, Object> StaffFormMap = new HashMap<String, Object>(); 
	    //存储公务交通
	    List<Map<String, Object>> listOfficialTraffic = new ArrayList<Map<String,Object>>();
	    Map<String, Object> officialTrafficMap = new HashMap<String, Object>(); 
	    //存储物业管理
	    List<Map<String, Object>> listPropertyManager = new ArrayList<Map<String,Object>>();
	    Map<String, Object> propertyManagerMap = new HashMap<String, Object>(); 
	    //存储时间
	    List<Map<String, Object>> listTime = new ArrayList<Map<String,Object>>();
	    Map<String, Object> timeMap = new HashMap<String, Object>(); 
	    
	    int sum =1;
		for(Date reim:getReimbursementTime){
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        String reimbursementTimes = dateFormat.format(reim);
			 timeMap.put("timeMap"+sum++, reimbursementTimes);
		}
		listTime.add(timeMap);
	    
		for(Map<String, Object> mapTypeName:getReimbursementTypeName) {//遍历list报销类型
			for(String reimbursementTypeNames :mapTypeName.keySet()) {//得到每一个的报销类型
				//System.out.print(reimbursementTypeNames+":"+map.get(reimbursementTypeNames));
				reimbursementTypeName = (String) mapTypeName.get(reimbursementTypeNames);//每一个报销类型的名称
				Integer getReimbursementMoney = 0;
				int count = 0;
				
				for(Date reimbursementTime:getReimbursementTime){//遍历时间
					
					//System.out.println(reimbursementTime);
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			        String reimbursementTimes = dateFormat.format(reimbursementTime); //获得的date类型的值转成String类型
			       
			        //得到每一个时间段，每一个报销类型的钱
			        getReimbursementMoney = echatsDao.getReimbursementMoney(userId, reimbursementTypeName, reimbursementTimes);
			        //System.out.println("==="+getReimbursementMoney);
					
					if("domesticTravel".equals(reimbursementTypeNames)){//国内差旅
						count++;
						domesticTravelMap.put(reimbursementTypeNames+count, getReimbursementMoney);
					}else if("staffForm".equals(reimbursementTypeNames)){//员工福利
						count++;
						StaffFormMap.put(reimbursementTypeNames+count, getReimbursementMoney);
					}else if("officialTraffic".equals(reimbursementTypeNames)){//公务交通
						count++;
						officialTrafficMap.put(reimbursementTypeNames+count, getReimbursementMoney);
					}else if("propertyManager".equals(reimbursementTypeNames)){//物业管理
						count++;
						propertyManagerMap.put(reimbursementTypeNames+count, getReimbursementMoney);
					}
					
					
				}
				
				
			}
			listDomesticTravel.add(domesticTravelMap);
			listStaffForm.add(StaffFormMap);
			listOfficialTraffic.add(officialTrafficMap);
			listPropertyManager.add(propertyManagerMap);
		}
		
		//所有类型的名字
		map.put("getReimbursementTypeName", getReimbursementTypeName);
		//获得时间
		map.put("listTime", listTime);
		map.put("listDomesticTravel", listDomesticTravel);
		map.put("listStaffForm", listStaffForm);
		map.put("listOfficialTraffic", listOfficialTraffic);
		map.put("listPropertyManager", listPropertyManager);
		map.put("getReimbursementTime", getReimbursementTime);
		listPersonalAccount.add(map);
 		return listPersonalAccount;
	}
	
	

	/*@Override
	public List<Map<String, Object>> listPersonalMap(Integer userId,
			String businessTravelStartSection) {
		
		List<Map<String, Object>> listPersonalMap = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>(); 
		
		
		List<String> listStartSection = echatsDao.getStartSection(userId);
		for(String startSection:listStartSection){
			List<String> endSection = echatsDao.getEndSection(userId, startSection);
			
		}
		
		
		
		return null;
	}
*/
	@Override
	public List<String> getStartSection(Integer userId) {
		
		List<String> listStartSection = echatsDao.getStartSection(userId);
		return  listStartSection;
	}

	@Override
	public List<String> getEndSection(Integer userId,
			String businessTravelStartSection) {
		List<String> listEndSection = echatsDao.getEndSection(userId, businessTravelStartSection);
		//List<String> listStartSection = echatsDao.getStartSection(userId);
		
		
		return listEndSection;
	}

	



	

	

}
